package com.yoomiito.app.ui.my.angle

import android.text.*
import android.widget.EditText
import chihane.jdaddressselector.BottomDialog
import cn.droidlover.xdroidmvp.cache.SharedPref
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.model.my.AngleUserInfoData
import com.yoomiito.app.utils.L
import com.yoomiito.app.utils.NumInputFilter
import com.yoomiito.app.utils.ResUtil

class AngleUserInfoAdapter(data: List<AngleUserInfoData>?) : BaseQuickAdapter<AngleUserInfoData, BaseViewHolder>(R.layout.item_user_info, data) {
    private var dialog: BottomDialog? = null
    override fun convert(helper: BaseViewHolder?, item: AngleUserInfoData?) {
        helper?.setText(R.id.keyTv, item?.key)
        helper?.setText(R.id.valueEt, item?.value)
        helper?.setGone(R.id.flagIv, item?.isShowFlag!!)
        helper?.getView<EditText>(R.id.valueEt).let {
            it?.filters = arrayOf(InputFilter.LengthFilter(
                    when (helper?.layoutPosition) {
                        0 -> 20
                        1 -> 11
                        2 -> 18
                        3->1000
                        4, 5 -> 50
                        else -> 10000
                    }
            ))
            if (helper?.layoutPosition==1){
                it?.inputType = InputType.TYPE_CLASS_NUMBER
            }else{
                it?.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE
            }
            it?.isEnabled = item?.isEnable!!
            it?.setTextColor(ResUtil.getColor(if (item?.isEnable!!) R.color.color_balk_999999 else R.color.color_balk_333333))
            val flag = helper?.layoutPosition != 3
            it?.hint = if (flag) "" else "请选择"
//            if (helper?.layoutPosition == 3) it?.keyListener = null
            it?.isLongClickable = flag
            it?.isFocusable = flag
            it?.isFocusableInTouchMode = flag
            it?.setOnClickListener {
                if (!flag) selectAddress()
            }

            it?.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(s: Editable?) {
                    data[helper?.layoutPosition!!].value = s.toString()
                }

                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                }
            })
        }
    }

    private fun selectAddress() {
        if (dialog == null) {
            val addressData = SharedPref.getInstance(App.getInstance()).getString(Constant.AddressInfo, "")
            dialog = BottomDialog(mContext)
            if (!TextUtils.isEmpty(addressData)) {
                dialog?.setAddressinfo(addressData)
            }
            dialog?.setOnAddressSelectedListener { province, city, county, _ ->
                data[3].value = province.name + city.name + county.name
                notifyItemChanged(3)
                dialog?.dismiss()
            }
        }
        dialog?.show()
    }
}